/*
 * File Name：PayMain.java
 * Copyright：Copyright 2008-2014 CiWong.Inc. All Rights Reserved.
 * Description： PayMain.java
 * Modify By：jejun
 * Modify Date：2014-6-12
 * Modify Type：Add
 */
package com.ciwong.mobilepay.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.content.res.ResourcesCompat;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.ciwong.mobilelib.R;
import com.ciwong.mobilepay.util.PayUtil;

/**
 * 支付主界面
 *
 * @author jejun
 * @version ciwong v.1.0 2014-6-12
 * @since ciwong v.1.0
 */
public class EPayMain extends LinearLayout {
    public static final int container = 0xff0f0f1;

    public static final int payOk = 0xff0f0f2;

    private int itemCount = 3;

    public EPayMain(Context context, int itemCount) {
        super(context);
        this.itemCount = itemCount;
        init();
    }

    private void init() {
        setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
        setBackgroundColor(0xffefeff4);
        setGravity(LinearLayout.VERTICAL);
        setOrientation(VERTICAL);
        setId(container);

        TitleBar tb = new TitleBar(getContext());
        addView(tb);// 添加标题栏

        ScrollView mScrollView = new ScrollView(getContext());
        mScrollView.setLayoutParams(new ViewGroup.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        addView(mScrollView);// 添加ScrollView

        LinearLayout container = new LinearLayout(getContext());
        container.setOrientation(LinearLayout.VERTICAL);
        // addView(container);
        container.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        PayDetailContainer pdc = new PayDetailContainer(getContext());
        container.addView(pdc);

        EPayMethodContainer pmc = new EPayMethodContainer(getContext(), itemCount);
        container.addView(pmc);

        View v = new View(getContext());
        v.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 0, 1.0f));
        container.addView(v);

        Button payOk = new Button(getContext());
        payOk.setId(EPayMain.payOk);
        LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int margin = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 15, dm);
        lp.bottomMargin = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 5, dm);
        lp.leftMargin = margin;
        lp.rightMargin = margin;
        lp.topMargin = margin;
        container.addView(payOk);
        payOk.setLayoutParams(lp);
//        Drawable enabled = PayUtil.getDrawableFromJar(
//                "/images_mobilepay/pay_btn_enabled.png", EPayMain.class);
        Drawable enabled = ResourcesCompat.getDrawable(getResources(), R.mipmap.pay_btn_enabled, null);

//        Drawable press = PayUtil.getDrawableFromJar(
//                "/images_mobilepay/pay_btn_press.png", EPayMain.class);
        Drawable press = ResourcesCompat.getDrawable(getResources(), R.mipmap.pay_btn_press, null);

//        Drawable normal = PayUtil.getDrawableFromJar(
//                "/images_mobilepay/pay_btn_normal.png", EPayMain.class);
        Drawable normal = ResourcesCompat.getDrawable(getResources(), R.mipmap.pay_btn_normal, null);


        Drawable selector = PayUtil.getDrawableSelector(new int[][]{
                {-android.R.attr.state_enabled},
                {android.R.attr.state_pressed}, {0}}, new Drawable[]{enabled,
                press, normal});
        payOk.setBackgroundDrawable(selector);
        payOk.setMinimumHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 50, dm));
        payOk.setText("确定购买");
        payOk.setEnabled(false);
        payOk.setTextColor(Color.WHITE);
        payOk.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);

        mScrollView.addView(container);// 添加内容
    }
}
